package bman.filecopy;

import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

import javax.swing.*;

import bman.tools.JException;
import bman.tools.net.Receiver;


import java.awt.BorderLayout;
import java.net.Socket;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class FileCopy extends javax.swing.JFrame implements KeyListener, MessageReceiver
{

	private JMenuItem helpMenuItem;

	private JMenu jMenu5;
	private JTextField jTextField1;
	private JEditorPane jEditorPane1;

	private JMenuItem deleteMenuItem;

	private JSeparator jSeparator1;

	private JMenuItem pasteMenuItem;

	private JMenuItem copyMenuItem;

	private JMenuItem cutMenuItem;

	private JMenu jMenu4;

	private JMenuItem exitMenuItem;

	private JSeparator jSeparator2;

	private JMenuItem closeFileMenuItem;

	private JMenuItem saveAsMenuItem;

	private JMenuItem saveMenuItem;

	private JMenuItem openFileMenuItem;

	private JMenuItem newFileMenuItem;

	private JMenu jMenu3;

	private JMenuBar jMenuBar1;
	
	FCC fcc = new FCC(this);

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args)
	{
		SwingUtilities.invokeLater(new Runnable()
		{
			public void run()
			{
				FileCopy inst = new FileCopy();
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
			}
		});
		
		
		
	}

	public FileCopy()
	{
		super();
		initGUI();
		
		addWindowListener(new WindowAdapter(){
			public void windowClosing(WindowEvent we)
			{
				System.exit(0);
			}
		});
	}

	private void initGUI()
	{
		try
		{
			{
				jEditorPane1 = new JEditorPane();
				getContentPane().add(jEditorPane1, BorderLayout.CENTER);
				display("");
			}
			{
				jTextField1 = new JTextField();
				getContentPane().add(jTextField1, BorderLayout.NORTH);
				jTextField1.setText("connect 127.0.0.1:777");
				jTextField1.addKeyListener(this);
			}
			this.setSize(500, 500);
		}
		catch (Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void clear()
	{
		jTextField1.setText("");
	}

	public void keyPressed(KeyEvent e)
	{
		System.out.println("keyPressed: "+e.getKeyCode());
		
		if(e.getKeyCode() == KeyEvent.VK_ENTER)
		{
			String cmd = jTextField1.getText();
			
			lastCommandIsCP = cmd.startsWith("cp ");
			copiedFiles.setLength(0);
			
			fcc.processCommand(cmd);
			clear();
		}
	}
	
	boolean lastCommandIsCP = false;
	StringBuffer copiedFiles = new StringBuffer();
	
	public void display(String s)
	{
		if(lastCommandIsCP)
		{
			String display = copiedFiles.toString()+s; 
			if(s.startsWith("Copied"))
			{
				copiedFiles.append(s+"\n");
			}
			s = display;
		}
		
		jEditorPane1.setText(s);
	}


	public void keyReleased(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void keyTyped(KeyEvent e)
	{
		// TODO Auto-generated method stub
		
	}

	public void receive(String message)
	{
		display(message);
	}

}
